using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Base;

namespace Drawing
{
    public class Drawing3DManager : DrawableGameComponent, IDisposable, IDrawing3DManager
    {
        private List<IShape> shapes = new List<IShape>();
        private List<IShape> nonPersistentShapes = new List<IShape>();
        private VertexDeclaration vertexDecl;
        private List<VertexPositionColor> buffer = new List<VertexPositionColor>();
        private BasicEffect effect;
        private int numberOfPoints = 0;

        public Drawing3DManager(Game game)
            : base(game)
        {

        }
        public IShape AddLine(Vector3 position, Vector3 position2, Color color, bool persistant)
        {
            IShape newShape = new Line(position,position2, color);
            if (persistant)
            {
                shapes.Add(newShape);
            }
            else
            {
                nonPersistentShapes.Add(newShape);
            }
            return newShape;
        }

        public override void Initialize()
        {
            IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)base.Game.Services.GetService(typeof(IGraphicsDeviceService));

            vertexDecl = new VertexDeclaration(graphicsService.GraphicsDevice, VertexPositionColor.VertexElements);
            effect = new BasicEffect(graphicsService.GraphicsDevice, null);// = base.Game.Content.Load<Effect>(@"shaders\simple");


            effect.VertexColorEnabled = true;



            base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
        public override void Draw(GameTime time)
        {


            List<IShape> totalShapes = new List<IShape>();
            totalShapes.AddRange(shapes);
            totalShapes.AddRange(nonPersistentShapes);
            if (totalShapes.Count != 0)
            {
                numberOfPoints = 0;

                foreach (IShape currentShape in totalShapes)
                {
                    foreach (VertexPositionColor currentVertex in currentShape.Vertices)
                    {
                        buffer.Add(currentVertex);
                        numberOfPoints++;
                    }
                }


                

                if (buffer.Count > 0)
                {

                    IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)base.Game.Services.GetService(typeof(IGraphicsDeviceService));
                    ICameraService camera = (ICameraService)base.Game.Services.GetService(typeof(ICameraService));
                    effect.Projection = camera.ProjectionMatrix;
                    effect.View = camera.ViewMatrix;

                    GraphicsDevice device = graphicsService.GraphicsDevice;

                    device.VertexDeclaration = vertexDecl;

                    effect.Begin();
                    effect.Techniques[0].Passes[0].Begin();

                    VertexPositionColor[] vertices = buffer.ToArray();
                    device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, vertices, 0, vertices.Length/2);

                    effect.Techniques[0].Passes[0].End();
                    effect.End();
                }
            }
            nonPersistentShapes.Clear();
            buffer.Clear();
        }

        protected virtual void Dispose(bool all)
        {
            if (vertexDecl != null)
            {
                vertexDecl.Dispose();
                vertexDecl = null;
            }
        }

        public virtual void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
